<template>
  <el-dialog
    :title="showTitle"
    :visible="showDialog"
    @close="close"
  >
    <el-form
      label-width="120px"
      ref="deptFrom"
      :model="deptFrom"
      :rules="deptRules"
    >
      <el-form-item
        prop="name"
        label-width="部门名称"
      >
        <el-input
          placeholder="2-10个字符"
          style="width: 80%"
          size="mini"
          v-model="deptFrom.name"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="deptCode"
        label-width="部门编码"
      >
        <el-input
          placeholder="2-10个字符"
          style="width: 80%"
          size="mini"
          v-model="deptFrom.code"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="managerId"
        label-width="部门负责人"
      >
        <el-select
          v-model="deptFrom.managerId"
          placeholder="请选择负责人"
          style="width: 80%"
          size="mini"
        >
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        prop="deptDescription"
        label-width="部门介绍"
      >
        <el-input
          placeholder="2-10个字符"
          type="textarea"
          style="width: 80%"
          size="mini"
          :rows="4"
          v-model="deptFrom.introduce"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-row
          type="flex"
          justify="center"
        >
          <el-col :span="12">
            <el-button
              size="mini"
              type="primary"
              @click="btnOk"
            >确定</el-button>
            <el-button
              size="mini"
              @click="cancelDept"
            >取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import {
  getDepartmentDetail,
  getDepartmentInfo,
  getManagerList,
  addDepartmentInfo,
  updateManager,
} from "@/api/department";
export default {
  name: "dept",
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    curNodeId: {
      type: Number,
      default: null,
    },
  },
  created() {
    this.getManagerList();
  },
  data() {
    return {
      deptFrom: {
        name: "",
        code: "",
        introduce: "",
        pid: 0,
        managerId: "",
      },
      managerList: [],
      deptRules: {
        name: [
          {
            required: true,
            message: "部门名字不能为空",
            trigger: "blur",
          },
          {
            min: 2,
            max: 10,
            message: "部门名字长度为2-10个字符",
            trigger: "blur",
          },
          {
            trigger: "blur",
            validator: async (rule, value, callback) => {
              let result = await getDepartmentInfo();
              if (this.deptFrom.id) {
                re = result.filter((item) => item.id !== deptFrom.id);
              }
              if (result.some((item) => item.name === value)) {
                callback(new Error("部门中已经存在该名称了"));
              } else {
                callback();
              }
            },
          },
        ],
        code: [
          {
            required: true,
            message: "部门编码不能为空",
            trigger: "blur",
          },
          {
            min: 2,
            max: 10,
            message: "部门编码长度为2-10个字符",
            trigger: "blur",
          },
          {
            trigger: "blur",
            validator: async (rule, value, callback) => {
              let result = await getDepartment();
              //编辑模式 判断是否已经存在部门编码
              if (this.deptFrom.id) {
                result = result.filter((item) => item.id !== this.deptFrom.id);
              }
              if (result.some((item) => item.code === value)) {
                callback(new Error("部门中已经有该编码了"));
              } else {
                callback();
              }
            },
          },
        ],
        managerId: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ], // 部门负责人id
        introduce: [
          {
            required: true,
            message: "部门介绍不能为空",
            trigger: "blur",
          },
          {
            min: 2,
            max: 500,
            message: "部门介绍长度为2-500个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    async getManagerList() {
      this.managerList = await getManagerList();
    },
    close() {
      this.$emit("update:showDialog", false);
    },
    btnOk() {
      this.$refs.deptFrom.validate(async isOk => {
        if (isOk) {
          let msg = "新增";
          if (this.deptFrom.id) {
            msg = "更新";
            updateManager(this.deptFrom);
          } else {
            console.log("id为2:" + this.curNodeId);
            addDepartmentInfo({ ...this.deptFrom, pid: this.curNodeId });
          }
          //通知父组件
          this.$emit("updateDepartment");
          //提示消息
          this.$message.success(`${msg}部门成功`);
          //关闭
          this.cancelDept();
        }
      });
    },
    cancelDept() {
      this.deptFrom = {
        code: "", // 部门编码
        introduce: "", // 部门介绍
        managerId: "", // 部门负责人id
        name: "", // 部门名称
        pid: "", // 父级部门的id
      };
      this.$refs.deptFrom.resetFields(); // 重置表单
      this.$emit("update:showDialog", false);
      this.$emit("updateDepartment", this.curNodeId);
    },
    async getDepartmentDetail() {
      this.deptFrom = await getDepartmentDetail(this.curNodeId);
    },
  },
  computed: {
    showTitle() {
      return this.deptFrom.id ? "编辑部门" : "新增部门";
    },
  },
};
</script>
